# -*- coding:utf-8 -*-

from django.conf import settings
from .models import *
import logging
logger = logging.getLogger('debug')

try:
    from dxuser.models import *

    class CompanyFileUri(models.Model):
        class Meta:
            pass

        #company = models.ForeignKey(Company)
        uri = models.CharField(max_length=255, blank=True, null=True)

    class UserFileUri(models.Model):
        class Meta:
            pass

        #user = models.ForeignKey(DXUser)
        uri = models.CharField(max_length=255, blank=True, null=True)

    class PublicFileUri(models.Model):
        class Meta:
            pass
        uri = models.CharField(max_length=255, unique=True)

    def fileuri_is_public(uri):
        return uri.startswith('pub/') or (len(PublicFileUri.objects.filter(uri=uri)) > 0)

    def user_can_view_fileuri(user, uri):
        user_has_it = len(UserFileUri.objects.filter(uri=uri).filter(user=user)) > 0
        if user.is_company_user:
            company_has_it = len(CompanyFileUri.objects.filter(uri=uri).filter(company=user.company)) > 0
        else:
            company_has_it = False
        return fileuri_is_public or (user_has_it or company_has_it)

except ImportError:
    logger.warn("dxuser not present. Not going on.")



